package com.mingye.components;

import com.mingye.board.Board;
import com.mingye.constants.LayoutConstants;
import com.mingye.model.Element;
import com.mingye.model.chess.Box;
import com.mingye.model.chess.Food;
import com.mingye.model.chess.Person;
import com.mingye.model.grid.BaseGrid;
import com.mingye.utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * 棋盘面板
 *
 * @author daixinyi
 */
public class BoardPanel extends JPanel {

    /**
     * 所属游戏窗口
     */
    private GameFrame gameFrame;

    /**
     * 棋盘对象
     */
    private Board board;

    public BoardPanel(GameFrame gameFrame, Board board) {
        this.setSize(LayoutConstants.BOARD_PANEL_WIDTH, LayoutConstants.BOARD_PANEL_HEIGHT);

        this.gameFrame = gameFrame;
        this.board = board;
    }

    @Override
    public void paint(Graphics g) {
        g.clearRect(0, 0, LayoutConstants.FRAME_WIDTH, LayoutConstants.FRAME_HEIGHT);

        BaseGrid[][] boardArr = board.getBoardArr();

        // 绘制线条
//        g.setColor(Color.BLACK);
//        int horLineCount = boardArr.length;
//        int verLineCount = boardArr[0].length;
//
//        for (int i = 0; i <= horLineCount; i++) {
//            g.drawLine(0, i * GameConstants.GRID_WIDTH_HEIGHT, 640, i * GameConstants.GRID_WIDTH_HEIGHT);
//        }
//        for (int i = 0; i <= verLineCount; i++) {
//            g.drawLine(i * GameConstants.GRID_WIDTH_HEIGHT, 0, i * GameConstants.GRID_WIDTH_HEIGHT, 640);
//        }

        // 绘制棋盘格子
        for (int i = 0; i < boardArr.length; i++) {
            Element[] subGrid = boardArr[i];
            for (int j = 0; j < subGrid.length; j++) {
                Element grid = boardArr[i][j];
                if (grid != null) {
                    BufferedImage gridImage = ImageUtils.loadImage(grid.getImagePath());
                     g.drawImage(gridImage, grid.getX() * LayoutConstants.GRID_WIDTH_HEIGHT ,grid.getY() * LayoutConstants.GRID_WIDTH_HEIGHT, this);
                }

            }
        }

        // 绘制食物
        List<Food> foodList = board.getFoodList();
        for (Food food : foodList) {
            BufferedImage foodImage = ImageUtils.loadImage(food.getImagePath());
            g.drawImage(foodImage, food.getX() * LayoutConstants.GRID_WIDTH_HEIGHT, food.getY() * LayoutConstants.GRID_WIDTH_HEIGHT, this);
        }

        // 绘制箱子
        List<Box> boxList = board.getBoxList();
        for (Box box : boxList) {
            BufferedImage boxImage = ImageUtils.loadImage(box.getImagePath());
            g.drawImage(boxImage, box.getX() * LayoutConstants.GRID_WIDTH_HEIGHT ,box.getY() * LayoutConstants.GRID_WIDTH_HEIGHT, this);
        }

        // 绘制人
        Person person = board.getPerson();
        BufferedImage personImage = ImageUtils.loadImage(person.getImagePath());
        g.drawImage(personImage, person.getX() * LayoutConstants.GRID_WIDTH_HEIGHT ,person.getY() * LayoutConstants.GRID_WIDTH_HEIGHT, this);
    }

    /**
     * 判断游戏是否通关胜利
     */
    public void checkWin() {
        int finishBoxCount = board.getFinishBoxCount();
        List<Box> boxList = board.getBoxList();
        if (finishBoxCount == boxList.size()) {
            // 通关
            gameFrame.victory();
        }
    }

    public Board getBoard() {
        return board;
    }

    public void setBoard(Board board) {
        this.board = board;
    }
}
